#-*- coding:utf-8 -*-
from tkinter import *
from tkinter.font import *
import tkinter.messagebox as box
from configparser import *
from threading import *
from lib.Total import *
from time import *
import time
import os
import ctypes

Ver = "0.0.2"

# 读取数据 执行设置
word = Tk()
sw = word.winfo_screenwidth()
sh = word.winfo_screenheight()
x = (sw-600) / 2
y = (sh-400) / 2
word.geometry('600x400+%d+%d'%(x,y)) # 窗口居中
word.iconbitmap("coits.ico")
word.title("操作验证 Operational verification")
CL = Label(bg = "white",width = 600,height = 400) # 窗口白底
CL.place(x = 0,y = 0,anchor = NW)
word.resizable(width=False, height=False)

curpath = os.path.dirname(os.path.realpath(__file__))
cfgpath = os.path.join(curpath, ".\\cot\\config.cot")
conf = ConfigParser()
conf.read(cfgpath, encoding="utf-8")
command_dis = conf.get("command","command_dis")

if command_dis == "True" :
    # 隐藏交互式窗口
    whnd = ctypes.windll.kernel32.GetConsoleWindow()
    ctypes.windll.user32.ShowWindow(whnd, 0)
    ctypes.windll.kernel32.CloseHandle(whnd)

if Get_archived("login.cot","#login.cot") == False :
    box.showerror(title = "丢失数据 Missing data:",message = "无法从云端获取必要数据!\nUnable to get necessary data from cloud!")
    Del_file()
    exit(0)

curpath = os.path.dirname(os.path.realpath(__file__))
cfgpath = os.path.join(curpath, ".\\tmp\\login.cot")
log = ConfigParser()
log.read(cfgpath, encoding="utf-8")
user_user = log.get("user","user")
user_key = log.get("user","password")
Del_file()

print("\n>>> 数据读取完成 Data read complete...")

# 验证管理员密码
com = Canvas(word,width = 600,height = 400,bg = "white",bd = 0)
PC_image = PhotoImage(file = ".\\cot\\PC.gif")
com.create_image(252,12,anchor = NW,image = PC_image)
com.place(x = -2,y = -2)
Administrators = Entry(word,justify =  CENTER,exportselection = False,bg = "white",bd = 0)
password = Entry(word,justify =  CENTER,exportselection = False,bg = "white",bd = 0,show = "*")
com.create_line(222,152,383,152,fill = "dodgerblue")
com.create_line(222,227,383,227,fill = "dodgerblue")
Administrators.place(x = 229,y = 125)
password.place(x = 229,y = 200)

def out() :
    ans = box.askyesno(title="操作验证 Operational verification:", message="确认退出登录界面?\nAre you sure to exit the login interface?")
    if ans == True:
        exit(0)
    else:
        return ()
def Administrators_check() :
    if Administrators.get() == "" and password.get() == "" :
        print("   .信息不全 Incomplete information")
        box.showerror(title="登录提示 Login prompt:", message="请输入管理员信息!\nPlease enter administrator information!")
    elif Administrators.get() == "" and password.get() != "" :
        print("   .名称不全 Incomplete name")
        box.showerror(title="登录提示 Login prompt:", message="请输入管理员名称!\nPlease enter administrator name!")
    elif Administrators.get() != "" and password.get() == "" :
        print("   .密码不全 Incomplete password")
        box.showerror(title="登录提示 Login prompt:", message="请输入管理员密码!\nPlease enter the administrator password!")
    else :
        if Administrators.get() != user_user or password.get() != user_key :
            print("   .信息错误 Information error")
            box.showerror(title="登录提示 Login prompt:", message="管理员的信息错误!\nAdministrator's information error!")
        else :
            print("   .通过操作验证 Through validation")
            word.destroy()

user_go = Button(word,activebackground = "lightgray",	activeforeground = "white",text = "Next",fg = "dodgerblue",bg = "white",relief = GROOVE,command = Administrators_check)
user_go.place(x = 400,y = 198)

word.protocol('WM_DELETE_WINDOW', out)  # 提示程序是否结束
word.mainloop()

#——————————主界面——————————#
print("\n>>> 面板准备就绪 Panel ready...")

# 窗口构建
coits = Tk()
sw = coits.winfo_screenwidth()
sh = coits.winfo_screenheight()
x = (sw-1000) / 2
y = (sh-625) / 2
coits.geometry('1000x625+%d+%d'%(x,y)) # 窗口居中
coits.iconbitmap("coits.ico")
coits.title("Peace dove - 控制台 Console")
CL = Label(bg = "white",width = 800,height = 500) # 窗口白底
CL.pack()
coits.resizable(width=False, height=False) # 设置窗口不可拖动

# 窗口操作定义
def close() :
    ans = box.askyesno(title = "操作确认 Operation confirmation:",message = "确认关闭 Peace dove 控制台?\nAre you sure to close the peace dove console?")
    if ans == True :
        coits.destroy()
    else :
        return ()

# 菜单栏
ctu = Menu(coits)
control = Menu(ctu,tearoff = 0)
ctu.add_cascade(label = "控制 Control",menu = control)

set = Menu(ctu,tearoff = 0)
ctu.add_cascade(label = "设置 Set",menu = control)

lib = Menu(ctu,tearoff = 0)
ctu.add_cascade(label = "库 Lib",menu = control)

hlep = Menu(ctu,tearoff = 0)
ctu.add_cascade(label = "帮助 Hlep",menu = control)
def check_updata() :
    if Get_archived("updata.cot","#updata.cot") == True :
        cloud_ver  = open(".\\tmp\\updata.cot").read()
        Del_file()
        if cloud_ver == Ver :
            box.showinfo(title = "程序更新 Updata:",message = "不需要进行更新!\nNo update required!")
        else :
            ans = box.askokcancel(title="程序更新 Updata:", message="发现版本 " + cloud_ver + "\nDiscovery version " + cloud_ver)
            if ans == True :
                pass

hlep.add_command(label = "检查更新",command = check_updata)
# 窗口功能


# 子线程启动


# 进入循环
coits['menu'] = ctu
coits.protocol('WM_DELETE_WINDOW',close) # 提示程序是否结束
coits.mainloop()